{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## Q1. 给定一个整数列表 [3, 5, 2, 7, 1]，找到其中的最大值和最小值，并打印两者之和。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "8\n"
     ]
    }
   ],
   "source": [
    "num_list = [3, 5, 2, 7, 1]\n",
    "print(max(num_list) + min(num_list))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:01.509599Z",
     "end_time": "2024-07-04T22:05:01.520314Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Q2. 使用 while 循环输出 1 到 10 的所有奇数。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "num = 1\n",
    "while num <= 10:\n",
    "    if num % 2 != 0:\n",
    "        print(num)\n",
    "    num += 1"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:01.524044Z",
     "end_time": "2024-07-04T22:05:01.536003Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Q3. 输入一个数字并将其转换为整数，如果输入的不是数字，则提示用户重新输入直到输入数字为止。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入的整数是： 12\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    try:\n",
    "        num = int(input(\"请输入一个数字：\"))\n",
    "        break\n",
    "    except ValueError:\n",
    "        print(\"输入有误，请重新输入！\")\n",
    "\n",
    "print(\"输入的整数是：\", num)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:01.538993Z",
     "end_time": "2024-07-04T22:05:05.289115Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Q4. 求 100 以内的素数。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]\n"
     ]
    }
   ],
   "source": [
    "prime_list = []\n",
    "\n",
    "for num in range(2, 101):\n",
    "    for i in range(2, num):\n",
    "        if num % i == 0:\n",
    "            break\n",
    "    else:\n",
    "        prime_list.append(num)\n",
    "\n",
    "print(prime_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.243627Z",
     "end_time": "2024-07-04T22:05:05.290110Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Q5. 请用至少两种不同办法计算1-100中奇数之和。"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "for i in range(1, 101):\n",
    "    if i % 2 != 0:\n",
    "        sum_odd_numbers += i\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.257334Z",
     "end_time": "2024-07-04T22:05:05.291098Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "i = 1\n",
    "while i <= 100:\n",
    "    if i % 2 != 0:\n",
    "        sum_odd_numbers += i\n",
    "    i += 1\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.269795Z",
     "end_time": "2024-07-04T22:05:05.291098Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = sum([i for i in range(1, 101) if i % 2 != 0])\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.286114Z",
     "end_time": "2024-07-04T22:05:05.302029Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = sum([i for i in range(1, 101, 2)])\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.303236Z",
     "end_time": "2024-07-04T22:05:05.391490Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "2500"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_odd_numbers = sum(list(range(1, 101, 2)))\n",
    "sum_odd_numbers"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.317611Z",
     "end_time": "2024-07-04T22:05:05.404767Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "i = 1\n",
    "while i <= 100:\n",
    "    sum_odd_numbers += i\n",
    "    i += 2\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.334123Z",
     "end_time": "2024-07-04T22:05:05.404767Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "numbers = list(range(1, 101))\n",
    "sum_odd_numbers = sum(numbers[::2])\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.347590Z",
     "end_time": "2024-07-04T22:05:05.405764Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "numbers = np.arange(1, 101)\n",
    "sum_odd_numbers = np.sum(numbers[numbers % 2 != 0])\n",
    "print(sum_odd_numbers)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.364002Z",
     "end_time": "2024-07-04T22:05:05.502946Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "2500"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_odd_numbers = 0\n",
    "count = 1\n",
    "while True:\n",
    "    sum_odd_numbers = sum_odd_numbers + count\n",
    "    count += 2\n",
    "    if count >= 100:\n",
    "        break\n",
    "\n",
    "sum_odd_numbers"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.504940Z",
     "end_time": "2024-07-04T22:05:05.551261Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "result = 0\n",
    "i = 0\n",
    "while i <= 100:\n",
    "    result += (i % 2) * i\n",
    "    i += 1\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-04T22:05:05.524287Z",
     "end_time": "2024-07-04T22:05:05.561217Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
